<?php
/**
 * Created by PhpStorm.
 * User: 柳迪
 * Date: 2018/4/23
 * Time: 8:08
 */

namespace app\index\controller;
use app\index\model\Url;
use think\Controller;
use think\Session;
use app\extra\lib\ImgCut;

class Login extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function login()
    {
        $user=['uname'=>input('post.uname'),'password'=>md5(input('post.password'))];
        if($result=db('users')->where('uname',$user['uname'])->where('password',$user['password'])->find())
        {
            Session::set('uid',$result['uid']);
            Session::set('lovelist',$result['lovelist']);
            $this->success('欢迎您，'.$result['nickname'].'。',url('index/index'));
        }
        $this->error('登陆失败');
        die();
    }

    public function getLoginStates()
    {
        if(Session::get('uid'))
        {
            $result=db('users')->field('uid,uname,nickname as name,hpic as pic')->find(Session::get('uid'));
            if($result)
            {
                $result['uid']=base64_encode($result['uid']);
                $result['state']=1;
            }
            else
            {
                $result['state']=0;
            }
            echo json_encode($result);
        }
        else
        {
            return json_encode(['state'=>0]);
        }
        die();
    }

    public function register()
    {
        $user=['uname'=>input('post.uname'),'nickname'=>input('post.nickname'),'password'=>input("password")];
        $validate= \think\Loader::validate('User');
        if(!$validate->check($user))
        {
            $this->error($validate->getError());
        }
        $result_find=db('users')->where('uname',$user['uname'])->find();
        if($result_find)
            $this->error('该用户名已存在， 请重新注册');
        $user['password']=md5($user['password']);
        $result=db('users')->insert($user);
        if($result)
            $this->success('注册成功',url('login/index'));
        die();
    }

    public function logout()
    {
        if(Session::get('uid'))
            Session::delete('uid');
    }

    public function signup()
    {
        return $this->fetch();
    }

    /**
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editInfo()
    {
        $info=input('post.info');
        $uid=Session::get('uid');
        $nickname=input('post.nickname');
        $name_img=null;
        try{
            if(request()->file('hpic'))
            {
                $file=request()->file('hpic');
                $info_img=$file->rule('uniqid')->validate(['ext'=>'jpg,jpeg,png'])->move(Url::ImgLocal());
                if($info_img)
                {
                    $name_img=$info_img->getFilename();
                }
                else
                {
                    var_dump($file->getError());
                }
                $name_img=$info_img->getFilename();
                $imgCut=new ImgCut(Url::ImgUrl().$name_img,$name_img);
                $imgCut->CutTo(128,128,Url::HeadLocal());
            }

            if(!($info==''))
                db('users')->update(['uid'=>$uid,'info'=>$info]);
            if(!($nickname==''))
                db('users')->update(['uid'=>$uid,'nickname'=>$nickname]);
            if(!($name_img==''))
                db('users')->update(['uid'=>$uid,'hpic'=>$name_img]);
            $this->redirect('index/index');
        }
        catch (\mysqli_sql_exception $exception)
        {
            $this->error('发生错误');
        }


        die();
    }

    public function changePass()
    {
        if(request()->isPost() && input("?post.yuanpass") && input("?post.newpass") && input("?post.uid"))
        {
            $yuanPass=md5(input("post.yuanpass"));
            $newPass=md5(input("post.newpass"));
            $uid=base64_decode(input("post.uid"));
            if($result=db('users')->where('uid',$uid)->where('password',$yuanPass)->select())
            {
                $user=['uname'=>'1111111','nickname'=>'1111111','password'=>input("post.newpass")];
                $validate= \think\Loader::validate('User');
                if(!$validate->check($user))
                {
                    echo json_encode(['status'=>0,'info'=>$validate->getError()]);
                    die();
                }
                db('users')->update(['uid'=>$uid,'password'=>$newPass]);
                echo json_encode(['status'=>1]);
                die();
            }
            else
            {
                echo json_encode(['status'=>0,'info'=>'对不起，您输入的原密码有误']);
            }

        }
        else
        {
            return json_encode(['status'=>0,'info'=>'对不起，网页错误，请联系管理员。']);
            die();
        }
    }

}